package com.yalemang.photovalut.ui.folder

import android.annotation.SuppressLint
import android.content.Intent
import androidx.recyclerview.widget.GridLayoutManager
import com.jeremyliao.liveeventbus.LiveEventBus
import com.yalemang.photovalut.base.BaseSkinActivity
import com.yalemang.photovalut.base.encryption.EncryptionManager
import com.yalemang.photovalut.base.encryption.Result
import com.yalemang.photovalut.base.ext.gone
import com.yalemang.photovalut.base.helper.CommonItemDecoration
import com.yalemang.photovalut.base.sql.SQLManager
import com.yalemang.photovalut.base.sql.entity.ResFile
import com.yalemang.photovalut.base.user.Skin
import com.yalemang.photovalut.databinding.ActivityPhotoFolderBinding
import com.yalemang.photovalut.ui.choice.ChoicePhotoActivity
import com.yalemang.photovalut.ui.folder.adapter.ResFileAdapter
import com.yalemang.photovalut.ui.preview.PreviewPhotoActivity
import java.io.File

class PhotoFolderActivity: BaseSkinActivity<ActivityPhotoFolderBinding, PhotoFolderPresenter>(),
    PhotoFolderContract.PhotoFolderView {

        companion object{
            lateinit var folderName: String
        }

    private var resFileList = ArrayList<ResFile>()
    private lateinit var mAdapter: ResFileAdapter
    private val sqlManager = SQLManager.getInstance()

    override fun switchSkin(skin: Skin) {
        mBinding.tbPhotoFolder.setBackgroundColor(skin.mainColor)
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun initData() {
        resFileList = sqlManager.getTableToResFiles(folderName) as ArrayList<ResFile>

        LiveEventBus.get(ChoicePhotoActivity.KEY_PHOTO_PATH_SET, HashSet::class.java)
            .observe(this){
                val photoPathSet = it as HashSet<String>
                for (path in photoPathSet){
                    val photoName = File(path).name
                    val newPath = EncryptionManager.getInstance().getEncryptionPhotoPath(folderName, photoName)
                    EncryptionManager.getInstance().encryption(path, newPath)
                        .subscribe(object: Result<String>(){
                            override fun result(t: String) {
                                val photoFile = File(t)
                                val resFile = ResFile()
                                resFile.path = t
                                resFile.fileFolderName = folderName
                                resFile.originalPath = path
                                resFile.encryptionTime = System.currentTimeMillis()
                                resFile.length = photoFile.length()
                                resFile.type = 1
                    sqlManager.addResFileToTable(folderName, resFile)
                                resFileList.add(resFile)
                                mAdapter.notifyDataSetChanged()
                            }
                        })
                }
            }
    }

    override fun initView() {
        mBinding.tbPhotoFolder.tvLeft.gone()
        mBinding.tbPhotoFolder.tvRight.gone()

        mAdapter = ResFileAdapter(this, resFileList, mPresenter.mModel.getSkin().mainColor)
        mBinding.rvPhotoFolder.layoutManager = GridLayoutManager(this, 2)
        mBinding.rvPhotoFolder.adapter = mAdapter
        mBinding.rvPhotoFolder.addItemDecoration(CommonItemDecoration.getItemDecoration(10))


        mBinding.ivPhotoFolder.setOnClickListener{
            val intent = Intent(this, ChoicePhotoActivity::class.java)
            startActivity(intent)
        }

        mAdapter.itemClickListener ={ _: ResFile, _: Int ->
            val intent = Intent(this@PhotoFolderActivity, PreviewPhotoActivity::class.java)
            val resPathList = ArrayList<String>()
            for (path in resFileList){
                resPathList.add(path.originalPath)
            }
            intent.putExtra(ChoicePhotoActivity.KEY_PHOTO_PATH_LIST, resPathList)
            startActivity(intent)
        }
    }

    override fun initMVP() {
        mPresenter = PhotoFolderPresenter()
        mPresenter.mView = this
        mPresenter.mModel = PhotoFolderModel()
    }
}